#include <iostream>
#include <string>
#include <vector>

// 基类 Character
class Character {
public:
    virtual void attack() const = 0; // 纯虚函数，使Character成为抽象基类
    virtual ~Character() {} // 虚析构函数，确保派生类的析构函数被正确调用
};

// 派生类 Warrior
class Warrior : public Character {
public:
    void attack() const override {
        std::cout << "Warrior attacks with a sword!" << std::endl;
    }
};

// 派生类 Mage
class Mage : public Character {
public:
    void attack() const override {
        std::cout << "Mage casts a spell!" << std::endl;
    }
};

// 派生类 Archer
class Archer : public Character {
public:
    void attack() const override {
        std::cout << "Archer shoots an arrow!" << std::endl;
    }
};

void performAttack(const Character* character) {
    character->attack();
}

int main() {
    Warrior warrior;
    Mage mage;
    Archer archer;

    // 存储基类指针的向量
    std::vector<Character*> characters = {&warrior, &mage, &archer};

    // 遍历并执行攻击
    for (const auto& character : characters) {
        performAttack(character);
    }

    return 0;
}